//
//  CapabilityAssessmentView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/1/12.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "CapabilityAssessmentView.h"
#import "NALLabelsMatrix.h"

@interface CapabilityAssessmentView () <didSelectCollectionDelegate>

@property (nonatomic, strong) UILabel *rightNumsLabel;
@property (nonatomic, strong) UILabel *allNumsLabel;
@property (nonatomic, strong) NALLabelsMatrix *mstrixView;          // 表格
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) CapabilityAssessmentModel *model;

@end

@implementation CapabilityAssessmentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 66.0, 40.0, 34.0, 32.0)];
    leftLabel.text = @"答对";
    leftLabel.textColor = RGB(40, 40, 40);
    leftLabel.font = [UIFont systemFontOfSize:16.0f];
    [self addSubview:leftLabel];
    
    self.rightNumsLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 32, 22.0, 64.0, 50.0)];
    self.rightNumsLabel.text = @"9";
    self.rightNumsLabel.textColor = MainThemeColor;
    self.rightNumsLabel.font = [UIFont systemFontOfSize:50.0f];
    self.rightNumsLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.rightNumsLabel];
    
    UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.rightNumsLabel.right, 40.0, 18.0, 32.0)];
    rightLabel.text = @"道";
    rightLabel.textColor = RGB(40, 40, 40);
    rightLabel.font = [UIFont systemFontOfSize:16.0f];
    [self addSubview:rightLabel];
    
    self.allNumsLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 70.0f, rightLabel.bottom, 64.0f, 28.0)];
    self.allNumsLabel.text = @"共10道题";
    self.allNumsLabel.font = [UIFont systemFontOfSize:15.0f];
    [self addSubview:self.allNumsLabel];
}

- (void)createNextView {
    NSNumber *columnsWidth = [NSNumber numberWithFloat:SCREEN_WIDTH / 4.0];
    self.mstrixView = [[NALLabelsMatrix alloc] initWithFrame:CGRectMake(0, self.allNumsLabel.bottom + 8.0, SCREEN_WIDTH, 100) andColumnsWidths:@[columnsWidth, columnsWidth, columnsWidth, columnsWidth]];
    [self.model.paperDatas enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.mstrixView addRecord:obj];
    }];
    [self addSubview:self.mstrixView];
    
    UILabel *answerCard = [[UILabel alloc] initWithFrame:CGRectMake(12, self.mstrixView.bottom + 24.0f, 80.0, 24.0)];
    answerCard.text = @"答题卡";
    answerCard.textColor = RGB(40, 40, 40);
    answerCard.font = [UIFont systemFontOfSize:16.0f];
    [self addSubview:answerCard];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, answerCard.bottom + 16.0f, SCREEN_WIDTH, 240) collectionViewLayout:layout];
    self.collectionView.modelClass = @"CardModel";
    self.collectionView.didDatagate = self;
    self.collectionView.minInteritemSpace = 32.0f;
    self.collectionView.minLineSpace = 16.0f;
    self.collectionView.edgeInsets = UIEdgeInsetsMake(0, 16.0f, 16.0f, 16.0f);
    [self.collectionView registerCellClass:@"CapabilityAssessmentCollectionViewCell" cellIdentifier:@"CapabilityAssessmentCollectionViewCellID"];
    
    [self addSubview:self.collectionView];
    
    CGFloat width = (SCREEN_WIDTH - 160.0f) / 5;
    NSUInteger count = self.model.user_answer_report.count;
    if (count % 5 == 0) {
        self.collectionView.height = (count / 5) *(width + 16.0f);
    } else {
        self.collectionView.height = (count / 5 + 1) *(width + 16.0f);
    }
    self.collectionView.sourceData = self.model.user_answer_report;
    self.collectionView.contentSize = CGSizeMake(SCREEN_WIDTH, self.collectionView.bottom);
}

- (void)setDataModel:(CapabilityAssessmentModel *)model {
    self.model = model;
    
    self.rightNumsLabel.text = model.right_count;
    NSString * str = @" ";
    if ([SettingObject objectIsValid:model.answer_count]){
        str = model.answer_count;
    }
    self.allNumsLabel.text = [NSString stringWithFormat:@"共%@道题", str];
    
    [self createNextView];
}

#pragma mark - didSelectCollectionDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectIndexPath:(NSIndexPath *)indexPath {
    if ([self.didDelegate respondsToSelector:@selector(didSelectAtIndexPath:)]) {
        [self.didDelegate didSelectAtIndexPath:indexPath];
    }
}

@end
